<?php

defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Advtype extends Controller_Admin_Based {

    /**
     * 添加广告位置
     */
    public function action_add() {
        $name_template = '<a href="/admin/advtype/edit/{id}" title="点击修改">{name}</a> <a href="/admin/advtype/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $conditional_name_template = '<a href="/admin/advtype/edit/{id}" title="点击修改">[ {name} ]</a> <a href="/admin/advtype/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $content_data['catalog_tree'] = Catehelp::get_left_tree($name_template, $conditional_name_template, array('table' => 'cheers_main_advtypes', 'id' => 'chr_advtypeid', 'name' => 'chr_advtypename'));
        $content_data['product_ids'] = array();
        $content_data['is_conditional'] = FALSE;
        $catalog_opt_template = array(
            'name_box' => ' <option value="{id}" {selected}>{indentation}{name}</option>',
            'indentation' => '&nbsp;|&nbsp;&nbsp;'
        );
        $content_data['catalog_opt'] = Catehelp::tree_to_str(Catehelp::get_tree($catalog_opt_template, 0, 0, array('table' => 'cheers_main_advtypes', 'id' => 'chr_advtypeid', 'name' => 'chr_advtypename')));

        if ($_POST) {
            
            $array = Validation::factory($_FILES);
            if ($array->check()) {
                $savepath = Uploads::handleupload( '/uploads/advtype', $_FILES['chr_advtypefile']);
            }

            $advtype = ORM::factory('cheers_main_advtype');
            $advtype->chr_advtypename = $_POST['chr_advtypename'];
            $advtype->chr_parentid = $_POST['chr_parentid'];
            if(isset($savepath)){
                 $advtype->chr_advtypefile = $savepath;
            }

            if ($advtype->check()) {
                $advtype->save();
                if ($advtype->saved()) {
                    Message::set('广告位添加成功');
                    Request::current()->redirect('/admin/advtype/add/');
                }
            }
        }
        $content = View::factory('admin/advert/advtype_add', $content_data)->render();
        $this->template = View::factory('admin/template')->set('content', $content);
    }

    /**
     * 修改广告位置
     */
    public function action_edit() {
        $name_template = '<a href="/admin/site/catalog/edit/{id}" title="点击修改">{name}</a> <a href="/admin/advtype/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $conditional_name_template = '<a href="/admin/advtype/edit/{id}" title="点击修改">[ {name} ]</a> <a href="/admin/advtype/delete/{id}" class="delete_catalog" title="点击删除">X</a>';
        $content_data['catalog_tree'] = Catehelp::get_left_tree($name_template, $conditional_name_template, array('table' => 'cheers_main_advtypes', 'id' => 'chr_advtypeid', 'name' => 'chr_advtypename'));
        $catalog_opt_template = array(
            'name_box' => '
            <option value="{id}" {selected}>{indentation}{name}</option>
            ',
            'indentation' => '&nbsp;|&nbsp;&nbsp;'
        );
        $id = $this->request->param('id');
        $advtype = ORM::factory('cheers_main_advtype', $id);

        $catalog_opt_array = Catehelp::get_tree($catalog_opt_template, 0, 0, array('table' => 'cheers_main_advtypes', 'id' => 'chr_advtypeid', 'name' => 'chr_advtypename'), $advtype->chr_parentid);
        $parents = Catehelp::get_parents('cheers_main_advtypes', $id);
        $node = &$catalog_opt_array;
        foreach ($parents as $node_id) {
            if ($node_id != $id) {
                $node = &$node[$node_id]['children'];
            }
        }
        unset($node[$id]);
        $content_data['catalog_opt'] = Catehelp::tree_to_str($catalog_opt_array);

        if ($_POST) {
            $array = Validation::factory($_FILES);
            if ($array->check()) {
                $savepath = Uploads::handleupload( '/uploads/advtype', $_FILES['chr_advtypefile']);
            }
            
           
            $data['chr_advtypename'] = Arr::get($_POST, 'chr_advtypename', '');
            $data['chr_parentid'] = Arr::get($_POST, 'chr_parentid', '');
            if(isset($savepath)){
                  $data['chr_advtypefile'] = $savepath;
            }

            $advtype->values($data);

            if ($advtype->check()) {
                $advtype->save();
                if ($advtype->saved()) {
                    message::set('广告分类修改成功!');
                    Request::current()->redirect('/admin/advtype/edit/' . $id);
                } else {
                    message::set('广告分类修改失败！', 'error');
                    Request::current()->redirect('/admin/advtype/edit/' . $id);
                }
            } else {
                message::set('广告分类修改失败！', 'error');
                Request::current()->redirect('/admin/advtype/edit/' . $id);
            }
        }
        $content = View::factory('admin/advert/advtype_edit', $content_data)->set('data', $advtype)->render();
        $this->template = View::factory('admin/template')->set('content', $content);
    }

    /**
     * 删除广告位置
     */
    public function action_delete() {
        $advtypeid = $this->request->param('id');
        $advtype = ORM::factory('cheers_main_advtype', $advtypeid);
        if ($advtype->loaded()) {
            $parentids_array = Catehelp::get_childs($advtypeid, 'cheers_main_advtypes', 'chr_advtypeid');
            $parentids = '"';
            foreach ($parentids_array as $key => $value) {
                $parentids .= $value . '","';
            }
            $parentids = rtrim(rtrim($parentids, '"'), ',');
            if (!empty($parentids)) {
                DB::query(Database::DELETE, "DELETE FROM cheers_main_advtypes WHERE chr_advtypeid in(" . $parentids . ")")->execute();
                Message::set('分类删除成功!');
                Request::current()->redirect('/admin/advtype/add/');
            } else {
                $advtype_del = $advtype->delete();
            }
            Message::set('分类删除成功!');
            Request::current()->redirect('/admin/advtype/add/');
        } else {
            Message::set('分类删除失败!', 'error');
            Request::current()->redirect('/admin/advtype/add/');
        }
    }

}

?>
